package com.safety.web.controller;

import com.safety.core.util.Data;
import com.safety.web.entity.HomeHealthNews;
import com.safety.web.entity.HomeList;
import com.safety.web.service.HomeHealthNewsService;
import com.safety.web.service.HomeListService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by pinky on 2016/12/9.
 */

@Controller
@RequestMapping("/home")
public class HomeListController {
    @Resource
    private HomeListService homeListService;
    @Resource
    private HomeHealthNewsService homeHealthNewsService;

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 首页不需要用户id的列表
     *
     * @return Object
     */
    @RequestMapping("/homePicList")
    @ResponseBody
    public Map<String, Object> homePicList() {
        //定义返回用
        Map<String, Object> result = new LinkedHashMap<>();
        //读取数据库
        List<HomeList> list = homeListService.homePicList();
        HomeHealthNews news = homeHealthNewsService.selectHealthNews(1);
        //定义区
        Map<String, Object> mapForArea1;
        Map<String, Object> mapForArea2;
        Map<String, Object> mapForArea3 = new LinkedHashMap<>();
        Map<String, Object> mapForArea4;
        Map<String, Object> healthTest_1 = new LinkedHashMap<>();
        Map<String, Object> healthTest_2 = new LinkedHashMap<>();

        List<Map<String, Object>> listForArea1 = new ArrayList<>();
        List<Map<String, Object>> listForArea2 = new ArrayList<>();
        List<Map<String, Object>> listForArea3 = new ArrayList<>();
        List<Map<String, Object>> listForArea4 = new ArrayList<>();
        List<Map<String, Object>> listForHealthTest = new ArrayList<>();

        //执行区
        try {
            if (list.size() != 0 && news.getMessage().length() != 0) {
                result.put("code", 200);
                for (HomeList getOnlyUrl : list) {
                    if (getOnlyUrl.getLocation().substring(0, 5).equals("area1")) {
                        mapForArea1 = new LinkedHashMap<>();
                        mapForArea1.put("picUrl1", Data.URL.base + getOnlyUrl.getPicUrl());
                        listForArea1.add(mapForArea1);
                    } else if (getOnlyUrl.getLocation().substring(0, 5).equals("area2")) {
                        mapForArea2 = new LinkedHashMap<>();
                        mapForArea2.put("picUrl2", Data.URL.base + getOnlyUrl.getPicUrl());
                        listForArea2.add(mapForArea2);
                    } else if (getOnlyUrl.getLocation().substring(0, 5).equals("area3")) {
                        mapForArea3 = new LinkedHashMap<>();
                        mapForArea3.put("picUrl3", Data.URL.base + getOnlyUrl.getPicUrl());
                        listForArea3.add(mapForArea3);
                    }else if (getOnlyUrl.getLocation().substring(0, 5).equals("area4")) {
                        mapForArea4 = new LinkedHashMap<>();
                        mapForArea4.put("picUrl4", Data.URL.base + getOnlyUrl.getPicUrl());
                        listForArea4.add(mapForArea4);
                    }
                }
                for (HomeList getOnlyUrl2 : list) {

                     if (getOnlyUrl2.getLocation().substring(0, 6).equals("health")) {
                        if(getOnlyUrl2.getLocation().substring(getOnlyUrl2.getLocation().length()-1).equals("1")){
                            healthTest_1.put(getOnlyUrl2.getLocation().substring(0,getOnlyUrl2.getLocation().length()-2), Data.URL.base + getOnlyUrl2.getPicUrl());
                        }else if(getOnlyUrl2.getLocation().substring(getOnlyUrl2.getLocation().length()-1).equals("2")){
                            healthTest_2.put(getOnlyUrl2.getLocation().substring(0,getOnlyUrl2.getLocation().length()-2), Data.URL.base + getOnlyUrl2.getPicUrl());
                        }
                    }
                }

                listForArea3.add(mapForArea3);
                listForHealthTest.add(healthTest_1);
                listForHealthTest.add(healthTest_2);

                //插入区
                result.put("healthNews", news.getMessage());
                result.put("area1", listForArea1);
                result.put("area2", listForArea2);
                result.put("area3", listForArea3);
                result.put("area4", listForArea4);
                result.put("healthTest", listForHealthTest);
            } else {
                result.put("code", 400);
            }
        } catch (Exception e) {
            System.out.println(e);
            result.put("code", 401);
        }
        return result;
    }

    /**
     * 首页需要用户id的列表
     *
     * @return Object
     */
    @RequestMapping("/homePicListWithId")
    @ResponseBody
    public Map<String, Object> homePicListWithId(int userId) {
        //定义返回用
        Map<String, Object> result = new LinkedHashMap<>();

        //读取数据库

        //定义区
        Map<String, Object> everyStepGetGold = new LinkedHashMap<>();
        Map<String, Object> healthPlanTemp = new LinkedHashMap<>();
        List<Map<String, Object>> healthPlan = new ArrayList<>();
        Map<String, Object> menstruation = new LinkedHashMap<>();
        Map<String, Object> hotPostTemp = new LinkedHashMap<>();
        List<Map<String, Object>> hotPost = new ArrayList<>();
        Map<String, Object> healthInformationTemp = new LinkedHashMap<>();
        List<Map<String, Object>> healthInformation = new ArrayList<>();

        //执行区
        result.put("code", 200);
        result.put("userId", userId);

        everyStepGetGold.put("text", "领取昨日奖励");
        everyStepGetGold.put("information", "走路挣钱，每天6元");
        everyStepGetGold.put("step", 1906);
        everyStepGetGold.put("calorie", 103);
        everyStepGetGold.put("fat", 11);

        healthPlanTemp.put("plan", "眼睛的头号大敌是酒精");
        healthPlanTemp.put("status", 1);
        healthPlan.add(healthPlanTemp);
        healthPlanTemp = new LinkedHashMap<>();
        healthPlanTemp.put("plan", "【初阶】上半身卷腹");
        healthPlanTemp.put("status", 0);
        healthPlan.add(healthPlanTemp);
        healthPlanTemp = new LinkedHashMap<>();
        healthPlanTemp.put("plan", "睡前喝杯葡萄酒");
        healthPlanTemp.put("status", 0);
        healthPlan.add(healthPlanTemp);
/**
 * 经期记录
 * 今天日期 周几 日期段 今天是第几天 小贴士
 */

        menstruation.put("status", "1");
        menstruation.put("today", "2016-12-14");
        menstruation.put("week", "Monday");
        menstruation.put("fromTo", "2016-12-14~2016-12-17");
        menstruation.put("howDay", 2);
        menstruation.put("tips", "多喝热水");

        hotPostTemp.put("userId", 16);
        hotPostTemp.put("userPic", "http://192.168.0.56:8080/homepic/1-1.jpg");
        hotPostTemp.put("userName", "沙壁");
        hotPostTemp.put("content", "我的天啊鸡翅什么的我最喜欢吃了 油油的哈哈哈");
        hotPostTemp.put("date", "2016-12-14");
        hotPostTemp.put("likeNum", 3514);
        hotPostTemp.put("commentNum", 2533);
        hotPost.add(hotPostTemp);
        hotPostTemp = new LinkedHashMap<>();
        hotPostTemp.put("userId", 17);
        hotPostTemp.put("userPic", "http://192.168.0.56:8080/homepic/1-2.jpg");
        hotPostTemp.put("userName", "素啦");
        hotPostTemp.put("content", "我的天啊可乐什么的我最喜欢喝了 甜甜的哈哈哈");
        hotPostTemp.put("date", "2016-12-14");
        hotPostTemp.put("likeNum", 4213);
        hotPostTemp.put("commentNum", 1234);
        hotPost.add(hotPostTemp);

        healthInformationTemp.put("articleId", 1);
        healthInformationTemp.put("articleTitle", "做到这两步，你就瘦了！");
        healthInformationTemp.put("articlePic", "http://192.168.0.56:8080/homepic/1-3.jpg");
        healthInformationTemp.put("articleClass", "边吃边减不是梦");
        healthInformationTemp.put("browseTime", 10352);
        healthInformation.add(healthInformationTemp);
        healthInformationTemp = new LinkedHashMap<>();
        healthInformationTemp.put("articleId", 2);
        healthInformationTemp.put("articleTitle", "做到这两步，你也瘦不了！");
        healthInformationTemp.put("articlePic", "http://192.168.0.56:8080/homepic/1-4.jpg");
        healthInformationTemp.put("articleClass", "边吃边减不是梦");
        healthInformationTemp.put("browseTime", 76894);
        healthInformation.add(healthInformationTemp);

        //插入区
        result.put("everyStepGetGold", everyStepGetGold);
        result.put("healthPlan", healthPlan);
        result.put("menstruation", menstruation);
        result.put("hotPost", hotPost);
        result.put("healthInformation", healthInformation);
        result.put("date_today",df.format(new Date()));
        return result;
    }
}
